package com.easthome.service.impl;

import com.easthome.constant.MessageConstant;
import com.easthome.mapper.MemberMapper;
import com.easthome.mapper.OrderMapper;
import com.easthome.mapper.OrderSettingMapper;
import com.easthome.pojo.Member;
import com.easthome.pojo.Order;
import com.easthome.pojo.OrderSetting;
import com.easthome.result.Result;
import com.easthome.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 保存订单
     *  提交预约信息
     *  体检预约方法处理逻辑比较复杂，需要进行如下业务处理：
     *  1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
     *  2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
     *  3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
     *  4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
     *  5、预约成功，更新当日的已预约人数
     * @param map
     * @return
     */
    @Override
    public Result saveOrder(Map<String, String> map) {
        // 检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        OrderSetting orderSetting = orderSettingMapper.queryOrderSettingByDate(map.get("orderDate"));
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            //预约已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //根据手机号检查当前用户是否为会员
        String telephone = map.get("telephone");
        Member member = memberMapper.queryMemberByTelephone(telephone);
        if (member == null) {
            //用户不是会员，自动完成注册并进行预约
            member = new Member();
            member.setName(map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard(map.get("idCard"));
            member.setSex(map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.addMember(member);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐）
        Order order = orderMapper.queryOrderByMemberIdAndOrderDate(member.getId(), map.get("orderDate"), map.get("setmealId"));
        if (order != null) {
            //用户重复预约
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
        Order newOrder = new Order();
        newOrder.setMemberId(member.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date orderDate = new Date(simpleDateFormat.parse(map.get("orderDate")).getTime());
            newOrder.setOrderDate(orderDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        newOrder.setOrderType("微信预约");
        newOrder.setOrderStatus("未到诊");
        newOrder.setSetmealId(Integer.parseInt(map.get("setmealId")));
        //保存预约信息
        orderMapper.addOrder(newOrder);
        //更新当日的已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        return new Result(true, MessageConstant.ORDER_SUCCESS);
    }
}
